import { getMetadataArgsStorage } from 'typeorm';
import { getMetadataStorage } from 'class-validator';

  // 递归获取所有父类
const getAllParents = (target: Function): Function[] => {
    const parent = Object.getPrototypeOf(target.prototype)?.constructor;
    return parent ? [parent, ...getAllParents(parent)] : [];
};
/**
 * 递归获取尸体了字段信息，
 * @param entity 实体类
 * @returns 
 */
const getInheritedFieldDescriptions = (entity: Function)=> {
  const storage = getMetadataArgsStorage();

  // 获取当前实体及其所有父类
  const targets = [entity, ...getAllParents(entity)];

  return storage.columns
    .filter(col => targets.includes(col.target as Function))
    .map(col => col);
}
export default getInheritedFieldDescriptions;